package com.example.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.domain.DataNode;
import com.example.auth.mapper.DataNodeConfigMapper;
import com.example.auth.service.DataNodeService;
import com.example.config.CacheService;
import com.example.config.exception.GlobalExcetion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author CETC
* @description 针对表【data_node_config】的数据库操作Service实现
* @createDate 2025-10-03 10:19:15
*/
@Service
public class DataNodeServiceImpl extends ServiceImpl<DataNodeConfigMapper, DataNode>
    implements DataNodeService {


    private static final String DATA_NODE_CACHE_NAME_ID = "dataNode_ID";

    private static final String DATA_NODE_CACHE_NAME_SERVERNAME = "dataNode_ServerName";

    private static final String DATA_NODE_CACHE_NAME_LIST = "dataNode_List";

    @Autowired
    private CacheService cacheService;

    @Override
    public DataNode getByIdCache(String id) {
        if(cacheService.containsKey(DATA_NODE_CACHE_NAME_ID,id)){
            return cacheService.get(DATA_NODE_CACHE_NAME_ID,id,DataNode.class);
        }else {
            DataNode dataNode=this.getById(id);
            cacheService.put(DATA_NODE_CACHE_NAME_ID,id,dataNode);
            return dataNode;
        }
    }

    @Override
    public IPage<DataNode> page(Integer pageNum, Integer pageSize, String nodeName) {
        LambdaQueryWrapper<DataNode> lqw = new LambdaQueryWrapper<>();
        lqw.like(DataNode::getNodeName, nodeName);
        IPage<DataNode> dataNodeConfigIPage=this.page(new Page(pageNum, pageSize), lqw);
        return dataNodeConfigIPage;
    }

    @Override
    public DataNode getByNodeServerName(String serverName) {
        if(cacheService.containsKey(DATA_NODE_CACHE_NAME_SERVERNAME,serverName)){
            return cacheService.get(DATA_NODE_CACHE_NAME_SERVERNAME,serverName,DataNode.class);
        }else {
            LambdaQueryWrapper<DataNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DataNode::getNodeServerName,serverName);
            DataNode dataNode=this.getOne(lambdaQueryWrapper);
            cacheService.put(DATA_NODE_CACHE_NAME_SERVERNAME,serverName,dataNode);
            return dataNode;
        }
    }

    @Override
    public List<DataNode> getDataNodeListByNodeNo(String nodeNo) {
        if(cacheService.containsKey(DATA_NODE_CACHE_NAME_LIST,nodeNo)){
            Object value= cacheService.get(DATA_NODE_CACHE_NAME_LIST,nodeNo);
            if(value!=null){
                return (List<DataNode>)value;
            }
            return new ArrayList<>();
        }else {
            LambdaQueryWrapper<DataNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DataNode::getNodeNo,nodeNo);
            List<DataNode> dataNodes=this.list(lambdaQueryWrapper);
            cacheService.put(DATA_NODE_CACHE_NAME_LIST,nodeNo,dataNodes);
            return dataNodes;
        }
    }

    @Override
    public DataNode saveDataNode(DataNode dataNode) throws GlobalExcetion {
        DataNode node=this.getByNodeServerName(dataNode.getNodeServerName());
        if(node!=null){
            throw new GlobalExcetion(1,"服务节点已存在");
        }
        //清空列表缓存
        cacheService.evict(DATA_NODE_CACHE_NAME_LIST,dataNode.getNodeNo());
        this.baseMapper.insert(dataNode);
        return dataNode;
    }

    @Override
    public DataNode updateDataNode(DataNode dataNode) {
        //清空列表缓存
        cacheService.evict(DATA_NODE_CACHE_NAME_LIST,dataNode.getNodeNo());
        //清空id
        cacheService.evict(DATA_NODE_CACHE_NAME_ID,dataNode.getId());
        this.baseMapper.updateById(dataNode);
        return dataNode;
    }

    @Override
    public List<DataNode> getByIds(List<String> idList) {
        LambdaQueryWrapper<DataNode> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DataNode::getId,idList);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }


}




